var max_lat;
var max_long;
var min_lat;
var min_long;
var allowedBounds;

var center_lat;
var center_long;

var markIcon = new Array();
/*Create icons for the markers _______________________________________________
___________________________________________________________________________*/
function createMarks(){
	for(j=0;j<btype.length;j++){
		markIcon[j] = new Array(20);
		for(i=0; i<20; i++){
			ico            = new GIcon();
			ico.image      = "img/buildings/"+btype[j]+i+".png";		
			ico.iconSize   = new GSize(20, 34);
			ico.iconAnchor = new GPoint(9, 34);	
			markIcon[j][i] = ico;
		}	
	}
}

/*Get the index of an element in the array ___________________________________*/
function getIndex(elem,mybtype) {
	var myPosition=-1;
	for (i=0;i<mybtype.length;i++) {
		if(mybtype[i]==elem) {
			myPosition = i;
			break;
		}
	}
	return myPosition;
}

/*Initialize global variables of the maps______________________________________
___________________________________________________________________________*/
function init_js(val_max_lat, val_min_lat, val_max_long, val_min_long, val_center_lat, val_center_long){
	max_lat  = val_max_lat;
	min_lat  = val_min_lat;
	max_long = val_max_long;
	min_long = val_min_long;	
	
	center_lat  = val_center_lat;
	center_long = val_center_long; 
	
  	//The allowed region which the whole map must be within__________________
  	//alert(min_lat+","+min_long+" >>> "+max_lat+","+max_long+" >>> "+center_lat+","+center_long);
	allowedBounds = new GLatLngBounds(new GLatLng(min_lat ,min_long), new GLatLng(max_lat ,max_long));
	
	createMarks();
}

/*Re-center the map _________________________________________________________
_____________________________________________________________________________*/
function checkBounds(map) {
	if (allowedBounds.contains(map.getCenter())) {
		return;
	}
	
	var C = map.getCenter();
	var X = C.lng();
	var Y = C.lat();

	var AmaxX = allowedBounds.getNorthEast().lng();
	var AmaxY = allowedBounds.getNorthEast().lat();
	
	var AminX = allowedBounds.getSouthWest().lng();
	var AminY = allowedBounds.getSouthWest().lat();
		
	if (X < AminX) {X = AminX+Math.abs((center_long-AminX)/2);}
	if (X > AmaxX) {X = AmaxX-Math.abs((center_long-AmaxX)/2);}
	if (Y < AminY) {Y = AminY+Math.abs((center_lat-AminY)/2);}
	if (Y > AmaxY) {Y = AmaxY-Math.abs((center_lat-AmaxY)/2);}
	map.setCenter(new GLatLng(Y,X));
}

/*BUY PROPERTY ______________________________________________________________
 * ________________________________________________________________________*/
function buyProperty(marker_id,prop_val) {
	var baseElems = document.getElementsByTagName('base');
	var base      = ""
	if(baseElems.length>0) {base = baseElems[0].href;}
	
	//create the message with AJAX for buy____________________________________
	jQuery.ajax({
		type:"POST",
		url: base+'Ajax/buyProperty/'+marker_id+"/"+prop_val+"/",
		success: function(data) {
			dataVect = data.split("^");
			alert(dataVect[0]);
			if(parseInt(dataVect[1])==0){
				setTimeout('location.reload()',1000);	
			}
	  	}
	});
	return false;	
}

/*SET FOR SALE______________________________________________________________
 * ________________________________________________________________________*/
function setForSale(property_id){
	value = $('#forsale').val();
	value = 1-value;
	var baseElems = document.getElementsByTagName('base');
	var base      = ""
	if(baseElems.length>0) {base = baseElems[0].href;}
	
	//update the property in DB____________________________________
	jQuery.ajax({
		type:"POST",
		url: base+'Ajax/setForsale/'+property_id+"/"+value+"/",
		success: function(data) {
			alert(data);
	  	}
	});
	return false;	
}

/*GET DESCRIPTION______________________________________________________________
 * ________________________________________________________________________*/
function getDestruction() {
	value = $('#destruction_name').val();
	
	var baseElems = document.getElementsByTagName('base');
	var base      = ""
	if(baseElems.length>0) {base = baseElems[0].href;}
	
	//update the property in DB____________________________________
	jQuery.ajax({
		type:"POST",
		url: base+'Ajax/getDestructionInfo/'+value+"/",
		success: function(data) {
			eval(data);
			$('#description_div').html("");
			$('#description_div').append('<div class="label_lf">Description</div>'+
				'<div class="field_lf">'+
					'<input type="hidden" id="destruction_price" name="destruction_price" value="'+destructions[3]+'" />'+
					'<input type="hidden" id="destruction_impact" name="destruction_impact" value="'+destructions[2]+'" />'+
					'<input type="hidden" id="destruction_id" name="destruction_id" value="'+destructions[4]+'" />'+					
					'<input type="hidden" id="destruction_duration" name="destruction_duration" value="'+destructions[5]+'" />'+					
					'<div><img src="'+destructions[1]+'"/></div>'+
					'<div>'+destructions[0]+'</div><br/>'+
					'<div><span class="strong">Impact:</span> -'+destructions[2]+'% out of building condition</div>'+
					'<div><span class="strong">Price:</span> '+destructions[3]+'</div>'+
				'</div>'+
				'<div class="clear">&nbsp;</div>');
	  	}
	});
}

function getUpgradePrice(old_value) {
	value = $('#building_cond').val();
	var baseElems = document.getElementsByTagName('base');
	var base      = ""
	if(baseElems.length>0) {base = baseElems[0].href;}
	jQuery.ajax({
		type:"POST",
		url: base+'Cron/getUpgradePrice/'+value+"/"+old_value+"/",
		success: function(data) {
			$('#upgrade_val').html('');
			$('#upgrade_val').append('<div class="label_lf">Upgrade Value: </div>'+
					'<div class="field_lf">'+data+'</div>'+
			'<div class="clear">&nbsp;</div>');
		}
	});
}
/*create account functions____________________________________________________
 * _________________________________________________________________*/
function findFriends() {
	var baseElems = document.getElementsByTagName('base');
	var base      = ""
	window.open("find_friends.html","find_friends", 
		"menubar=0,resizable=0,location=0,status=0,scrollbars=1,width=600,height=450");
}

function getFreindsDiv(){
	var baseElems = document.getElementsByTagName('base');
	var base      = ""
	if(baseElems.length>0) {base = baseElems[0].href;}
	game_id = $('#groups_list').val();
	if(game_id>0) {
		jQuery.ajax({
			type:"POST",
			url: base+'Ajax/getGameFriends/'+game_id+'/',
			success: function(data) {
				$('#friends_div').html("");
				$('#friends_div').append(data);
			}
		});
	}
}

function chooseGroup(){
	game_id = $('#groups_list').val();
	if(game_id>0){
		var baseElems = document.getElementsByTagName('base');
		var base      = ""
		if(baseElems.length>0) {base = baseElems[0].href;}
		jQuery.ajax({
			type:"POST",
			url: base+'Ajax/setTheGame/'+game_id+'/',
			success: function(data) {
				alert('Your choice has been saved!');
			}
		});
		setTimeout("self.close();",2000);
	} else {
		$('#message').html("<br/>&raquo; Please select a group!<br/>");
	}
}

function makeNewGroup(cities){
	var fname=prompt("Please insert a game names followed by  a <<,>> and the city name "+ 
		" from the list: "+cities+"\n\n"+
		"Please enter the name of the new group:","Group Name,City Name");
	namesVect = fname.split(',');
	if(namesVect.length==2){
		$('#game_name').val(jQuery.trim(namesVect[0]));
		$('#city_name').val(jQuery.trim(namesVect[1]));
	}
}


function getPriceForBuilding(bcond,rworth){
	btype = $('#building_type').val();
	if(btype.length > 1){
		var baseElems = document.getElementsByTagName('base');
		var base      = ""
		if(baseElems.length>0) {base = baseElems[0].href;}
		jQuery.ajax({
			type:"POST",
			url: base+'Ajax/setPriceForBuilding/'+bcond+'/'+rworth+"/"+btype+"/",
			success: function(data) {
				$('#building_price').html(data);
			}
		});
	}
}

